import java.io.*;
import java.util.*;

/*
 * Class name: UserIO
 * Description: Used to make an array list from a txt file full of user names
 */
public class UserIO {

	public static ArrayList<User> getUsers(){
		ArrayList<User> users = new ArrayList<User>();
		BufferedReader in = getReader("users.txt");
		User user = readUser(in);
		while (user != null){
			users.add(user);
			user = readUser(in);
		}
		return users;
	}
	
	private static BufferedReader getReader(String name){
		BufferedReader in = null;
		try {
			File file = new File(name);
			in = new BufferReader(new FileReader(file));
		}
		catch (FileNotFoundException e){
			System.out.println("The file in question does not exist, please check the path and try again.");
			System.exit(0);
		}
		catch (IOException e){
			System.out.println("I/O Exception.")
			System.exit(0);
		}
		return in;
	}
	
	private static User readUser(BufferedReader in){
		String name;
		String username;
		String password;
		String line = "";
		String data[];
		
		try{
			line = in.readLine();
		}
		catch (IOException e){
			System.out.println("I/O Exception.")
			System.exit(0);
		}
		if (line == null){
			return null;
		}
		else{
			data = line.split("\t");
			name = data[0];
			username = data[1];
			password = data[2];
			return new User(name, username, password);
		}
	}
}